import os
import wave
# import soundfile as sf
# from vosk import Model, KaldiRecognizer

# VOSK_MODEL_PATH = 'app/audio/vosk-model-cn'  # 例如: vosk-model-small-cn-0.22

class SpeechRecognizer:
    def __init__(self, model_path=None):
        # if not os.path.exists(model_path):
        #     raise RuntimeError(f"Vosk模型目录不存在: {model_path}")
        # self.model = Model(model_path)
        pass

    def recognize(self, audio_path):
        # wav_path = self._ensure_wav(audio_path)
        # wf = wave.open(wav_path, "rb")
        # if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getframerate() not in [8000, 16000, 44100]:
        #     raise ValueError("只支持单声道16bit 8k/16k/44.1kHz wav文件")
        # rec = KaldiRecognizer(self.model, wf.getframerate())
        # rec.SetWords(True)
        # result = ''
        # while True:
        #     data = wf.readframes(4000)
        #     if len(data) == 0:
        #         break
        #     if rec.AcceptWaveform(data):
        #         res = rec.Result()
        #         result += self._extract_text(res)
        # result += self._extract_text(rec.FinalResult())
        # wf.close()
        # return result.strip()
        return '[语音识别功能已禁用]'

    # def _extract_text(self, vosk_result):
    #     import json
    #     try:
    #         obj = json.loads(vosk_result)
    #         return obj.get('text', '') + ' '
    #     except Exception:
    #         return ''

    # def _ensure_wav(self, audio_path):
    #     if audio_path.endswith('.wav'):
    #         return audio_path
    #     wav_path = '/tmp/upload.wav'
    #     data, samplerate = sf.read(audio_path)
    #     sf.write(wav_path, data, samplerate, subtype='PCM_16')
    #     return wav_path 